package com.bawei.module_home.ui.devices

import android.graphics.Color
import android.view.View
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityDeviceDetailBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.DEVICE_DETAIL_PAGE)
class DeviceDetailActivity : BaseActivity<BaseViewModel,ActivityDeviceDetailBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_device_detail

    private val mViewModel: HomeServiceViewModel by viewModels()
    @Autowired
    @JvmField
    var deviceId:  Int = 0
    override fun initView() {
        super.initView()
        initTopBar()
    }

    override fun initData() {
        super.initData()
        mViewModel.getDeviceDetail(deviceId)
    }
    override fun initListener() {
        super.initListener()
        mViewModel.deviceDetailState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"失败"+it.message)
                }
                Loading -> {}
                Success -> {
                    val data=mViewModel.deviceDetail.value
                    mBinding.device=data
                    if (data?.status=="正常"){
                        mBinding.pack.setText("已分配")
                        mBinding.status.setText("已巡检>")
                        mBinding.status.setTextColor(Color.RED)
                    }else{
                        mBinding.pack.setText("未分配")
                        mBinding.status.setText("未巡检>")
                        mBinding.status.setTextColor(Color.parseColor("#1E88E5"))
                        mBinding.startCheck.setOnClickListener{
                            mViewModel.postDeviceStatus(deviceId,"正常")
                        }
                    }
                }
            }
        }
        mViewModel.deviceStatusState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"失败"+it.message)
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"成功")
                    if (mViewModel.deviceStatus.value=="正常"){
                        mBinding.pack.setText("已分配")
                        mBinding.status.setText("已巡检>")
                        mBinding.status.setTextColor(Color.RED)
                    }
                }
            }
        }
    }
    fun initTopBar() {
        Glide.with(this).load(com.bawei.lib_common.R.drawable.back_other).into(mBinding.toolbar.back)
        mBinding.toolbar.more.visibility = View.GONE
        mBinding.toolbar.title.setText("设备详情")
        mBinding.toolbar.back.setOnClickListener {
            finish()
        }
    }

}